package org.smog.core.validation.impl;

import org.smog.core.validation.NotNullAndEmpty;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Description: Created by IntelliJ IDEA.
 * @project_name: smogTemp
 * @time: 2020-05-16 11:45
 * @email: 17685306043@163.com
 * @author: huangZhongYao
 */
public class NotNullAndEmptyValidatorImpl implements ConstraintValidator<NotNullAndEmpty, Object> {

	@Override
	public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
		if (o instanceof List) {
			if (((List) o).size() > 0) {
				return true;
			}
		} else if (o instanceof Set) {
			if (((Set) o).size() > 0) {
				return true;
			}
		} else if (o instanceof String) {
			if (((String) o).length() > 0) {
				return true;
			}
		} else if (o instanceof MultipartFile) {
			if (!((MultipartFile) o).isEmpty()) {
				return true;
			}
		} else {
			if (o instanceof Long || o instanceof Integer || o instanceof BigDecimal || o instanceof Boolean || o instanceof Double || o instanceof Float || o instanceof Enum || o instanceof Date) {
				return true;
			}
		}
		return false;
	}
}
